<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          @click="handleResetSearch()"
          style="float:right;margin-right: 15px"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="渠道：">
            <el-select
              v-model="listQuery.channel"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in channelList"
                :label="item.commonDesc"
                :key="item.commonCode"
                :value="item.commonCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="位置：">
            <el-select
              v-model="listQuery.classified"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in classifiedType"
                :key="item.commonCode"
                :label="item.commonDesc"
                :value="item.commonCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <!-- 数据列表 -->
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAdd()"
        style="margin-left: 20px"
      >发布内容</el-button>
    </el-card>

    <!-- 数据展示 -->
    <div class="table-container">
      <el-table
        ref="orderTable"
        :data="contentlist"
        style="width: 100%;"
        empty-text="请选择位置后，点击查询搜索"

        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column
          label="ID"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column
          label="标题"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
        >
          <template slot-scope="scope">{{ statusList[scope.row.status] }}</template>
        </el-table-column>
        <el-table-column
          label="生效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.stime | formatCreateTime }}</template>
        </el-table-column>
        <el-table-column
          label="失效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.etime | formatCreateTime }}</template>
        </el-table-column>
        <el-table-column
          label="是否推荐"
          width="140"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              active-value="1"
              inactive-value="0"
              v-model="scope.row.isRecommend"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
            >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>

  </div>
</template>

<script>
import {
  contentList,
  commonList,
  contentUpdate,
  contentGetById,
  deleteRecommend,
  saveRecommend
} from '@/api/content'
import { formatDate } from '@/utils/date'

const defaultListQuery = {
  title: '',
  source: '',
  channel: '',
  classified: '',
  status: '',
  isRecommend: '',
  pageNum: 1,
  pageSize: 10
}
export default {
  data () {
    return {
      statusList: ['', '未生效', '未生效', '未生效', '生效中', '已失效'],
      channelList: [],
      listQuery: Object.assign({}, defaultListQuery),
      total: null,
      contentlist: [],
      newsList: null,
      // 选择的复选框
      multipleSelection: [],
      classifiedType: [],
      listLoading: false,
      // 排序对话框
      sortDialogVisible: false,
      sortDialogData: { sort: 0, id: null }
    }
  },
  created () {
    this.getChannelType()
    this.getSourseType()
  },
  methods: {
    handleStatusChange (index, row) {
      console.log(row.isRecommend)
      if (row.isRecommend === '1') {
        const list = { ...row }
        console.log(list)
        // creatTime 不用传
        // name 就是title
        // sourceId 就是id
        // releaseTime 就是createtime
        // sourceType 是页面中带的
        // channel 也是页面中带的
        const data = {
          channel: list.channel,
          releaseTime: list.createTime,
          name: list.title,
          sourceId: list.id,
          sourceType: '03'
        }
        console.log(2)
        saveRecommend(data).then((response) => {
          console.log(response.data)
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        })
      }
      if (row.isRecommend === '0') {
        console.log(row)
        const params = {
          sourceId: row.id,
          sourceType: '03'
        }
        // console.log(row);
        deleteRecommend(params).then((response) => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        })
      }
    },
    getChannelType () {
      const params = {
        commonType: 'channel'
      }
      // 获取枚举
      commonList(params).then((response) => {
        // const list = response.data;
        console.log(response.data)
        this.channelList = response.data
      })
    },
    getSourseType () {
      const params = {
        commonType: 'classified'
      }
      // 获取枚举
      commonList(params).then((response) => {
        // const list = response.data;
        // console.log(response.data);
        this.classifiedType = response.data
      })
    },
    // 重置
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    // 筛选搜索
    handleSearchList () {
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 内容发布
    handleAdd () {
      this.$router.push({ path: '/cont-ms/addContent' })
    },
    // 编辑
    handleUpdate (index, row) {
      this.$router.push({
        path: '/cont-ms/UpdateContent',
        query: { id: row.id }
      })
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    getList () {
      console.log(this.listQuery)
      // this.listQuery.orderBy = "create_time";
      this.listLoading = true
      contentList(this.listQuery).then((response) => {
        this.listLoading = false
        // console.log(response.data);
        this.contentlist = response.data.list
        console.log(this.contentlist)
        this.total = response.data.total
        // console.log(this.total)
      })
    }
  },
  filters: {
    formatCreateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  }
}
</script>

<style scoped>
</style>
